<template>
  <div class="register-container">
    <div class="register-card">
      <div class="brand-header">
        <h1 class="brand-name">闪团购</h1>
        <p class="brand-slogan">限时抢购 · 品质保障</p>
      </div>

      <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form">
        <h2 class="form-title">创建新账户</h2>

        <el-form-item prop="username">
          <el-input
            v-model="registerForm.username"
            type="text"
            auto-complete="off"
            placeholder="请输入手机号/邮箱"
            prefix-icon="el-icon-user-solid"
            class="custom-input"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="registerForm.password"
            type="password"
            auto-complete="off"
            placeholder="设置登录密码"
            prefix-icon="el-icon-lock"
            class="custom-input"
            @keyup.enter.native="handleRegister"
            show-password
          />
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="registerForm.confirmPassword"
            type="password"
            auto-complete="off"
            placeholder="确认登录密码"
            prefix-icon="el-icon-lock"
            class="custom-input"
            @keyup.enter.native="handleRegister"
            show-password
          />
        </el-form-item>

        <el-form-item prop="code" v-if="captchaEnabled" class="captcha-item">
          <el-input
            v-model="registerForm.code"
            auto-complete="off"
            placeholder="输入验证码"
            prefix-icon="el-icon-message"
            class="custom-input captcha-input"
            @keyup.enter.native="handleRegister"
          />
          <div class="register-code">
            <img :src="codeUrl" @click="getCode" class="register-code-img"/>
            <span class="refresh-text" @click="getCode">刷新验证码</span>
          </div>
        </el-form-item>

        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          class="register-btn"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">立即注册</span>
          <span v-else>注册中...</span>
        </el-button>

        <div class="login-promotion">
          <span class="login-text">已有闪团购账号？</span>
          <router-link :to="'/login'" class="login-link">
            直接登录 <i class="el-icon-arrow-right"></i>
          </router-link>
        </div>
      </el-form>

      <div class="social-login">
        <div class="divider">
          <span class="divider-text">或使用以下方式登录</span>
        </div>
        <div class="social-icons">
          <el-button circle class="social-icon wechat">
            <i class="el-icon-chat-line-round"></i>
          </el-button>
          <el-button circle class="social-icon alipay">
            <i class="el-icon-wallet"></i>
          </el-button>
          <el-button circle class="social-icon qq">
            <i class="el-icon-chat-dot-square"></i>
          </el-button>
        </div>
      </div>
    </div>

    <div class="footer">
      <span>©2023 闪团购 版权所有 | 隐私政策 | 服务条款</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login"

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"))
      } else {
        callback()
      }
    }
    return {
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '账号长度必须为2-20个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 6, max: 20, message: "密码长度必须为6-20个字符", trigger: "blur" },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符：< > \" ' \\\ |", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      captchaEnabled: true
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.registerForm.uuid = res.uuid
        }
      })
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          register(this.registerForm).then(res => {
            const username = this.registerForm.username
            this.$alert("<font color='red'>恭喜您，账号 " + username + " 注册成功！</font>", '注册成功', {
              dangerouslyUseHTMLString: true,
              type: 'success'
            }).then(() => {
              this.$router.push("/login")
            }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background: url('https://images.unsplash.com/photo-1556740738-b6a63e27c4df?ixlib=rb-1.2.1&auto=format&fit=crop&w=1920&q=80') no-repeat center center;
  background-size: cover;
  position: relative;
  padding: 40px 20px;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 0;
  }
}

.register-card {
  width: 100%;
  max-width: 480px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  padding: 40px;
  position: relative;
  z-index: 1;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.brand-header {
  text-align: center;
  margin-bottom: 30px;

  .brand-name {
    font-size: 36px;
    font-weight: 700;
    color: #ff4d4d;
    margin-bottom: 8px;
    letter-spacing: 2px;
  }

  .brand-slogan {
    font-size: 16px;
    color: #666;
    margin: 0;
    font-weight: 400;
  }
}

.form-title {
  font-size: 24px;
  color: #333;
  text-align: center;
  margin-bottom: 30px;
  font-weight: 600;
  position: relative;

  &::after {
    content: '';
    display: block;
    width: 80px;
    height: 4px;
    background: linear-gradient(to right, #ff4d4d, #f9cb28);
    margin: 12px auto 0;
    border-radius: 2px;
  }
}

.custom-input {
  margin-bottom: 20px;

  ::v-deep .el-input__inner {
    height: 52px;
    line-height: 52px;
    border-radius: 12px;
    padding-left: 50px;
    border: 1px solid #e0e0e0;
    transition: all 0.3s;
    background: rgba(255, 255, 255, 0.9);
    font-size: 15px;

    &:focus {
      border-color: #ff6b6b;
      box-shadow: 0 0 0 2px rgba(255, 107, 107, 0.2);
    }

    &::placeholder {
      color: #bbb;
    }
  }

  ::v-deep .el-input__prefix {
    left: 15px;
    i {
      font-size: 20px;
      color: #999;
    }
  }
}

.captcha-item {
  display: flex;
  align-items: center;
  margin-bottom: 25px;

  .captcha-input {
    flex: 1;
    margin-right: 15px;
  }

  .register-code {
    width: 140px;
    display: flex;
    flex-direction: column;
    align-items: center;

    .register-code-img {
      width: 100%;
      height: 52px;
      border-radius: 8px;
      cursor: pointer;
      border: 1px solid #eee;
      margin-bottom: 5px;
      object-fit: cover;
    }

    .refresh-text {
      font-size: 12px;
      color: #ff6b6b;
      cursor: pointer;
      transition: all 0.2s;

      &:hover {
        text-decoration: underline;
        color: #ff4d4d;
      }
    }
  }
}

.register-btn {
  width: 100%;
  height: 52px;
  font-size: 16px;
  border-radius: 12px;
  background: linear-gradient(90deg, #ff4d4d, #f9cb28);
  border: none;
  margin-top: 10px;
  color: white;
  font-weight: 600;
  letter-spacing: 1px;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(255, 77, 77, 0.3);

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(255, 77, 77, 0.4);
  }

  &:active {
    transform: translateY(0);
  }
}

.login-promotion {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 25px;

  .login-text {
    font-size: 14px;
    color: #666;
    margin-right: 8px;
  }

  .login-link {
    color: #ff6b6b;
    font-weight: 500;
    text-decoration: none;
    display: flex;
    align-items: center;
    transition: all 0.2s;

    i {
      margin-left: 4px;
      transition: all 0.2s;
    }

    &:hover {
      color: #ff4d4d;

      i {
        transform: translateX(3px);
      }
    }
  }
}

.social-login {
  margin-top: 40px;

  .divider {
    display: flex;
    align-items: center;
    margin-bottom: 20px;

    &::before, &::after {
      content: '';
      flex: 1;
      height: 1px;
      background: linear-gradient(to right, transparent, #ddd, transparent);
    }

    .divider-text {
      padding: 0 15px;
      color: #999;
      font-size: 14px;
    }
  }

  .social-icons {
    display: flex;
    justify-content: center;
    gap: 20px;

    .social-icon {
      width: 48px;
      height: 48px;
      font-size: 20px;
      border: none;
      transition: all 0.3s;

      &.wechat {
        background: #07C160;
        color: white;

        &:hover {
          transform: translateY(-3px);
          box-shadow: 0 5px 15px rgba(7, 193, 96, 0.3);
        }
      }

      &.alipay {
        background: #1677FF;
        color: white;

        &:hover {
          transform: translateY(-3px);
          box-shadow: 0 5px 15px rgba(22, 119, 255, 0.3);
        }
      }

      &.qq {
        background: #12B7F5;
        color: white;

        &:hover {
          transform: translateY(-3px);
          box-shadow: 0 5px 15px rgba(18, 183, 245, 0.3);
        }
      }
    }
  }
}

.footer {
  margin-top: 40px;
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 13px;
  position: relative;
  z-index: 1;

  span {
    padding: 8px 15px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 20px;
  }
}

@media (max-width: 768px) {
  .register-card {
    padding: 30px 20px;
    width: 90%;
  }

  .brand-name {
    font-size: 28px !important;
  }

  .form-title {
    font-size: 20px !important;
  }
}
</style>
